package cn.tedu.egweibo2.controller;

import cn.tedu.egweibo2.mapper.WeiboMapper;
import cn.tedu.egweibo2.pojo.dto.WeiboDTO;
import cn.tedu.egweibo2.pojo.entity.Weibo;
import cn.tedu.egweibo2.pojo.vo.UserVO;
import cn.tedu.egweibo2.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo2.pojo.vo.WeiboListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "02.微博模块")
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;

    /**
     * 发布微博功能
     * 接口地址:/v1/weibo/insert
     * 请求方式:POST
     * 请求数据类型:application/json
     * 响应数据类型:int
     */
    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO == null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }

    /**
     * 首页微博列表功能
     * 接口地址:/v1/weibo/selectIndex
     * 请求方式:GET
     * 请求数据类型:application/x-www-form-urlencoded
     * 响应数据类型:见响应参数
     */
    @ApiOperation(value = "首页微博列表功能")
    @GetMapping("selectIndex")
    public List<WeiboListVO> selectIndex(){
        List<WeiboListVO> list = weiboMapper.selectWeiboList();
        return list;
    }

    /**
     * 微博详情功能
     * 接口地址:/v1/weibo/selectById
     * 请求方式:GET
     * 请求数据类型:application/x-www-form-urlencoded
     * 响应数据类型:见响应参数
     */
    @ApiOperation(value = "微博详情功能")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id", value = "微博编号", required = true, example = "200", dataType = "int")
    public WeiboDetailVO selectById(int id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeiboById(id);
        return weiboDetailVO;
    }
}
